#include <libpub.h>

#define DELAY   1000

extern "C"{
void msleep(unsigned int ms);
int Headset_GetState();
int Headset_GetHookSwitch();
}
int chk_headset_rmv_event_d();
int chk_headset_ins_event_d();
int chk_headset_button_pressed();
int chk_headset_button_released();

//cmd: test headset
//cmd: test headset hookswitch
int _Do_Test_Headset_Ins_Rmv(char *param)
{
    TOUT(1, 1, printf("\n _Do_Test_Headset_Ins_Rmv(%s)\n", param));

    if (*param) {//test headset hookswitch
        TOUT(1, 1, printf("\nTest headset button pressed/released state.\n"));
        chk_headset_button_pressed();
        chk_headset_button_released();


    }
    else {//test headset
        TOUT(1, 1, printf("\nTest headset insertion/removal operation.\n"));
        chk_headset_ins_event_d();
        chk_headset_rmv_event_d();

    }
    return 0;
}

int chk_headset_ins_event_d()
{
    int ins_state = 0;
    printf("\nwait for headset insertion\n");

    while(1){
        msleep(DELAY);
        ins_state = Headset_GetState();
        if(1 == ins_state){
            printf("\nDetect headset insertion!\n");
            break;
        }
        printf(".");
    }
    return 0;
}

int chk_headset_rmv_event_d()
{
    int rmv_state = 0;
    printf("\nWait for headset removal");

    while(1){
        msleep(DELAY);
        rmv_state = Headset_GetState();
        if(0 == rmv_state){
            printf("\nDetect headset removal!\n");
            break;
        }
        printf(".");
    }
    return 0;
}


int chk_headset_button_pressed()
{
    int press_state = 0;
    printf("\nWait for headset button pressed\n");

    while(1){
        msleep(DELAY);
        press_state = Headset_GetHookSwitch();
        if(1 == press_state){
            printf("\nDetect headset button pressed!\n");
            break;
        }
        printf(".");
    }
    return 0;
}

int chk_headset_button_released()
{
    int press_state = 0;
    printf("\nWait for headset button chk_headset_button_released\n");

    while(1){
        msleep(DELAY);
        press_state = Headset_GetHookSwitch();
        if(0 == press_state){
            printf("\nDetect headset button chk_headset_button_released!\n");
            break;
        }
        printf(".");
    }
    return 0;
}
